﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using STKMIS.Model;
using STKMIS.DAL;
using System.Data;

namespace STKMIS.BLL
{
    public class PurchaseItem
    {
        public PurchaseItem() { }

        /// <summary>
        /// 插入采购明细
        /// </summary>
        /// <param name="purchaseItemInfo">采购明细信息</param>
        /// <param name="tran"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static bool InsertPurchaseItem(PurchaseItemModel purchaseItemInfo, DBTransaction tran, ref string msg)
        {
            int executedRows = 0;
            bool isTrue = true;
            try
            {

                List<SqlDBParameter> param = new List<SqlDBParameter>();

                param.Add(new SqlDBParameter("@PurchaseID", DbType.Int32, purchaseItemInfo.PurchaseID));
                param.Add(new SqlDBParameter("@ProductID", DbType.Int32, purchaseItemInfo.ProductID));
                param.Add(new SqlDBParameter("@PurchaseMoney", DbType.Decimal, purchaseItemInfo.PurchaseMoney));

                param.Add(new SqlDBParameter("@PurchaseItemID", DbType.Int32, ParameterDirection.Output));
                param.Add(new SqlDBParameter("@Msg", DbType.String, ParameterDirection.Output, 50));


                executedRows = SqlDBServer.ExecuteStoredProc("proc_PurchaseItemInsert", param, tran.Transaction, SqlDBServer.SqlConnStringModelDb);

                if (executedRows <= 0)
                {
                    isTrue = false;
                }
                else
                {
                    if (param[3].Value != null)
                    {
                        purchaseItemInfo.PurchaseItemID = Convert.ToInt32(param[3].Value.ToString());
                    }
                    if (param[4].Value != null)
                    { 
                        msg = param[4].Value.ToString();
                    }
                    else
                    {
                        isTrue = false;
                    }
                }

                return isTrue;
            }
            catch
            {
                return false;
            }
        }
    }
}
